package com.xmindguoguo.boot.modular;

import java.util.LinkedList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.xmindguoguo.boot.common.constant.GlobalConstant;
import com.xmindguoguo.boot.config.properties.ConfigurationKit;
import com.xmindguoguo.boot.core.BaseController;
import com.xmindguoguo.boot.core.shiro.ShiroKit;
import com.xmindguoguo.boot.modular.system.model.TSystemMenuModel;
import com.xmindguoguo.boot.modular.system.model.TSystemNoticeModel;
import com.xmindguoguo.boot.modular.system.service.ITSystemMenuService;
import com.xmindguoguo.boot.modular.system.service.ITSystemNoticeService;
import com.xmindguoguo.boot.util.ToolUtil;
import com.xmindguoguo.boot.util.json.JsonResult;

/**
 * 首页控制器
 * 
 * @ClassName IndexController
 * @author <a href="892042158@qq.com" target="_blank">于国帅</a>
 * @date 2018年11月14日 下午5:45:29
 *
 */
@Controller
@RequestMapping
public class IndexController extends BaseController {
    @Autowired
    private ITSystemNoticeService systemNoticeService;
    @Autowired
    private ITSystemMenuService systemMenuService;

    // 跳转到首页
    @RequestMapping("")
    public String index() {
        return "index";
    }

    // 跳转到我的桌面
    @RequestMapping("welcome")
    public String welcome() {
        // 通知公告的展示
        Wrapper<TSystemNoticeModel> wrapper = new EntityWrapper<>();
        wrapper.ne("status", GlobalConstant.MODEL_DEL);
        wrapper.orderBy("createTime", false);
        wrapper.last("limit 5");// mysql 只查询前5条
        List<TSystemNoticeModel> noticeList = systemNoticeService.selectList(wrapper);
        request.setAttribute("noticeList", noticeList);
        return "welcome";
    }

    @RequestMapping(value = "test", method = RequestMethod.GET)
    public String error() {
        request.setAttribute(GlobalConstant.Const.ERROR_MSG, "test");
        return "/common/error/unauthorized";
    }

    /**
     * 首页左侧菜单
     */
    @RequestMapping("/tSystemMenu/selectSystemMenuList")
    @ResponseBody
    public Object selectSystemMenuList(TSystemMenuModel model) {
        Wrapper<TSystemMenuModel> wrapper = new EntityWrapper<>();
//         其他选项 不显示
        Long autoMenuId = ConfigurationKit.CONFIG.getLong("autoMenuId", 1L);
        if (ToolUtil.isNotEmpty(autoMenuId)) {
            wrapper.ne("pid", autoMenuId);
            wrapper.ne("id", autoMenuId);
        }
        wrapper.eq("menuType", GlobalConstant.MenuType.MENU); // 只查询是菜单的
        wrapper.ne("status", GlobalConstant.MODEL_DEL);
        wrapper.orderBy("orderNum ", false);
        wrapper.orderBy("createTime ", false);
        List<TSystemMenuModel> menuList = systemMenuService.selectList(wrapper);
        List<TSystemMenuModel> menuHasPerList = new LinkedList<>();
        if (CollectionUtils.isNotEmpty(menuList)) {
            for (TSystemMenuModel tSystemMenuModel : menuList) {
                // 需要权限的 判断权限
                if (!tSystemMenuModel.getType().equals(GlobalConstant.MenuControlType.LONGIN_AND_PER)
                        || ShiroKit.hasPermission(tSystemMenuModel.getReqUrl())) {
                    menuHasPerList.add(tSystemMenuModel);
                }
            }
        }
        return JsonResult.sendOk(menuHasPerList);
    }
}
